package com.system.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.bean.User;
import com.system.common.bean.User;
import com.system.common.service.IUserService;
import com.system.common.util.PasswordUtil;
import com.system.common.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @DubboReference
    private IUserService userService;

    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        user.setPassword(PasswordUtil.getEncryption(user.getPassword()));
        userService.addUser(user);
        return Result.ok("添加用户成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteUser(id);
        return Result.ok("删除用户成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.updateUser(user);
        return Result.ok("修改用户成功");
    }

    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestBody User user) {
        userService.updateStatus(user.getId(), user.getStatus());
        return Result.ok("修改用户状态成功");
    }

    @GetMapping("/list")
    public Result list() {
        return Result.ok("获取用户列表成功",userService.getUserList());
    }

    @GetMapping("/page")
    public Result page(User user, 
                  @RequestParam(defaultValue = "1") Integer pageNum, 
                  @RequestParam(defaultValue = "10") Integer pageSize){
        return Result.ok(userService.getPage(user, new Page<>(pageNum, pageSize)));
    }

    @GetMapping("/getUserById/{id}")
    public Result getUserById(@PathVariable Integer id) {
        return Result.ok("获取用户成功",userService.getUserById(id));
    }

    @GetMapping("/getUserByUsername/{username}")
    public Result getUserByUsername(@PathVariable String username) {
        return Result.ok("获取用户成功",userService.getUserByUsername(username));
    }

    @GetMapping("/getUserWithAddress/{id}")
    public Result getUserWithAddress(@PathVariable Integer id) {
        return Result.ok("获取用户及其地址列表成功",userService.getUserWithAddress(id));
    }

    @GetMapping("/getUserWithAddressList")
    public Result getUserWithAddressList() {
        return Result.ok("获取用户列表及其地址成功",userService.getUserWithAddressList());
    }
}
